package com.hxbd.gxsec_data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxbd.gxsec_data.dao.NewsDao;
import com.hxbd.gxsec_data.dao.ResourceDao;
import com.hxbd.gxsec_data.pojo.FileDO;
import com.hxbd.gxsec_data.pojo.ImgDO;
import com.hxbd.gxsec_data.service.NewsService;
import com.hxbd.gxsec_data.utils.FileUtil;
import com.hxbd.gxsec_data.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service("newsService")
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsDao newsDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MinioUtil minioUtil;

    @Override
    public boolean addNewsData(Map<String, Object> map) {
        if (redisTemplate.hasKey("newsList")){
            redisTemplate.delete("newsList");
        }
        return newsDao.addNewsData(map);
    }

    @Override
    public ImgDO uploadFiles(MultipartFile file,String type) {
        String oldName = file.getOriginalFilename();
        String newName = FileUtil.renameToUUID(oldName);
            minioUtil.putObject("file",file,newName);
            String id = UUID.randomUUID().toString().replaceAll("-","");
            String url = "http://gxsec.org.cn/staticFile/file/"+newName;
            ImgDO imgDO = ImgDO.builder().imgId(id).url(url).build();
            //判断上传的图片是否是封面
        return type.equals("cover")
                ?(newsDao.uploadImg(imgDO)
                ?imgDO
                :null)
                :imgDO;
    }

    @Override
    public List<JSONObject> getAllNews() {
        if (redisTemplate.hasKey("newsList")){
            return redisTemplate.opsForList().range("newsList",0,-1);
        }else {
            List<JSONObject> newsList = newsDao.selectNewsDatasByParam();
            if(newsList.size()>0){
                redisTemplate.opsForList().leftPushAll("newsList",newsList);
            }
            return newsList;
        }
    }

    @Override
    public boolean saveUpdateNews(Map<String, Object> map) {
        if (redisTemplate.hasKey("newsList")){
            redisTemplate.delete("newsList");
        }
        return newsDao.updateNewsData(map);
    }

    @Override
    public boolean batchNews(Map<String, Object> map) {
        if (redisTemplate.hasKey("newsList")){
            redisTemplate.delete("newsList");
        }
        //新闻内容图片数组
        List<String> picList = (List<String>) map.get("url");
        for (String url : picList){
            deleteLocalPic(url);
        }
        //封面url
        String coverUrl = map.get("coverUrl").toString();
        deleteLocalPic(coverUrl);
        newsDao.batchUploadImg(coverUrl);

        //新闻id
        String newsId = map.get("newsId").toString();
        return newsDao.batchNewsData(newsId);
    }

    @Override
    public boolean batchUploadImg(String url) {
        deleteLocalPic(url);
        return newsDao.batchUploadImg(url);
    }

    public void deleteLocalPic(String url){
        minioUtil.removeObject("file",url.substring(url.lastIndexOf("/")+1));
    }

    @Override
    public List<JSONObject> getNewsByname(String name) {
        if(redisTemplate.hasKey("searchNews")&&redisTemplate.opsForHash().hasKey("searchNews",name)){
            return (List<JSONObject>)redisTemplate.opsForHash().get("searchNews",name);
        }else {
            List<JSONObject>  list = newsDao.getNewsByname(name);
            redisTemplate.opsForHash().put("searchNews",name,list);
            return list;
        }
    }
}
